package com.standardoperationapp.operationplan;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.standardoperationapp.R;
import com.standardoperationapp.app.BaseApplication;
import com.standardoperationapp.bean.CardPointStepBean;
import com.standardoperationapp.bean.FackDrakBean;
import com.standardoperationapp.databinding.ActivityProtectionBinding;
import com.standardoperationapp.global.GlobalVaries;
import com.standardoperationapp.global.GsonUtils;
import com.standardoperationapp.net.OnSuccessAndFaultListener;
import com.standardoperationapp.net.OnSuccessAndFaultSub;
import com.standardoperationapp.net.OperationApi;
import com.standardoperationapp.utils.AlarmUtil;
import com.standardoperationapp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联防防控页面
 */
public class ProtectionActivity extends AppCompatActivity {


    private int type; // 0结束联防防控  1开启联防防控
    private int planId;
    private int workPointId;
    private int id;
    private ActivityProtectionBinding binding;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                // 报警
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_protection);


        planId = getIntent().getIntExtra("planId", 0);
        workPointId = getIntent().getIntExtra("workPointId", 0);
        id = getIntent().getIntExtra("id", 0);
        type = getIntent().getIntExtra("type", 0);

        if (type ==0){
            binding.topBar.setTitle("结束联防防控");
            binding.start.setVisibility(View.GONE);
        } else if (type ==1){
            binding.topBar.setTitle("开启联防防控");
            binding.end.setVisibility(View.GONE);
        } else if (type ==2){
            binding.topBar.setTitle("人脸识别结果");
            binding.start.setVisibility(View.GONE);
            binding.end.setVisibility(View.GONE);
            binding.confirm.setVisibility(View.VISIBLE);
            getFaceAlcoholResult("face");
        } else if (type ==3){
            binding.topBar.setTitle("酒精检测结果");
            binding.start.setVisibility(View.GONE);
            binding.end.setVisibility(View.GONE);
            binding.confirm.setVisibility(View.VISIBLE);
            getFaceAlcoholResult("drink");
        }


        binding.start.setOnClickListener(view -> {
            GlobalVaries.plantId = planId;
            GlobalVaries.workPointId = workPointId;
            GlobalVaries.executableId = id;

            submitExecuteStatus(id);
            // 设置报警
            AlarmUtil.setOneTimeAlarm(this, BaseApplication.getApp().alarmIntent);
        });
        binding.end.setOnClickListener(view -> {
            submitExecuteStatus(id);
            AlarmUtil.cancelAlarm(this, BaseApplication.getApp().alarmIntent);
        });
        binding.confirm.setOnClickListener(view -> {
            submitExecuteStatus(id);
            finish();
        });
    }

    private void getFaceAlcoholResult(String type) {
        OperationApi.tabdutyinfo(type, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                FackDrakBean bean = GsonUtils.fromJson(result, FackDrakBean.class);
                List<String> data = new ArrayList<>();
                for (FackDrakBean.DataBean datum : bean.getData()) {
                    String str = "姓名：" +datum.getHname() + "\n";
                    if (type.equals("face")) {
                        str += "人脸识别：" + datum.getLoginStatus()  + "\n";
                        str += "人脸识别结果：" + datum.getFacecomp()  + "\n";
                    }else {
                        str += "酒精含量：" + datum.getDrinkValue()  + "\n";
                        str += "酒精结果："+ datum.getDrinkResult()+ "\n";
                        str += "检测时间："+ datum.getRecordDatetime()+ "\n";
                    }
                    data.add(str);
                }
                ArrayAdapter adapter = new ArrayAdapter<String>(ProtectionActivity.this, R.layout.item_point_step, data);//装配适配器
                binding.listView.setAdapter(adapter);
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast("获取结果失败："+errorMsg);
            }
        }));
    }


    /**
     * 执行状态确认
     */
    private void submitExecuteStatus(int pointExecutableMethodId) {
        Map<String, Object> map = new HashMap<>();
        map.put("planId", planId);
        map.put("workPointId", workPointId);
        map.put("pointExecutableMethodId", pointExecutableMethodId);
        map.put("executableStatus", 1);
        OperationApi.recordPliAndDetail(map, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }
}